package com.zhai.domain.activity.service.partake;

import com.zhai.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import com.zhai.domain.activity.model.entity.*;
import com.zhai.domain.activity.model.valobj.UserRaffleOrderStateVO;
import com.zhai.domain.activity.repository.IActivityRepository;
import com.zhai.types.enums.ResponseCode;
import com.zhai.types.exception.AppException;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 参与活动实现
 * @ClassName: RaffleActivityPartakeService
 * @Author: zhaiyongxin
 * @Date: 2025/2/28 10:32
 * @Version: 1.0
 */
@Service
public class RaffleActivityPartakeService extends AbstractRaffleActivityPartake{
    public RaffleActivityPartakeService(IActivityRepository activityRepository) {
        super(activityRepository);
    }

    private final SimpleDateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM");
    private final SimpleDateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 过滤活动账户 总额度 月额度 日额度
     * @param activityId
     * @param userId
     * @param currentDate
     * @return
     */
    @Override
    protected CreatePartakeOrderAggregate doFilterAccount(Long activityId, String userId, Date currentDate) {
        // 1.校验账户总额度
        ActivityAccountEntity activityAccountEntity = activityRepository.queryActivityAccountByUserId(activityId, userId);
        if (activityAccountEntity == null || activityAccountEntity.getTotalCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_QUOTA_ERROR.getInfo());
        }

        String month = dateFormatMonth.format(currentDate);
        String day = dateFormatDay.format(currentDate);

        // 2.校验账户月额度
        ActivityAccountMonthEntity activityAccountMonthEntity = activityRepository.queryActivityAccountMonthByUserId(activityId, userId, month);
        if (null != activityAccountMonthEntity && activityAccountMonthEntity.getMonthCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getInfo());
        }
        boolean isExistAccountMonth =  null != activityAccountMonthEntity;
        if (activityAccountMonthEntity == null) {
            activityAccountMonthEntity = new ActivityAccountMonthEntity();
            activityAccountMonthEntity.setUserId(userId);
            activityAccountMonthEntity.setActivityId(activityId);
            activityAccountMonthEntity.setMonth(month);
            activityAccountMonthEntity.setMonthCount(activityAccountEntity.getMonthCount());
            activityAccountMonthEntity.setMonthCountSurplus(activityAccountEntity.getMonthCount());
        }

        // 3.校验账户日额度
        ActivityAccountDayEntity activityAccountDayEntity = activityRepository.queryActivityAccountDayByUserId(activityId, userId, day);
        if (activityAccountDayEntity != null && activityAccountDayEntity.getDayCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getInfo());
        }
        boolean isExistAccountDay = activityAccountDayEntity != null;
        if (activityAccountDayEntity == null) {
            activityAccountDayEntity = new ActivityAccountDayEntity();
            activityAccountDayEntity.setUserId(userId);
            activityAccountDayEntity.setActivityId(activityId);
            activityAccountDayEntity.setDay(day);
            activityAccountDayEntity.setDayCountSurplus(activityAccountEntity.getDayCount());
            activityAccountDayEntity.setDayCount(activityAccountEntity.getDayCount());
        }

        // 4.构建聚合对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = new CreatePartakeOrderAggregate();
        createPartakeOrderAggregate.setUserId(userId);
        createPartakeOrderAggregate.setActivityId(activityId);
        createPartakeOrderAggregate.setActivityAccountEntity(activityAccountEntity);
        createPartakeOrderAggregate.setExistAccountMonth(isExistAccountMonth);
        createPartakeOrderAggregate.setActivityAccountMonthEntity(activityAccountMonthEntity);
        createPartakeOrderAggregate.setExistAccountDay(isExistAccountDay);
        createPartakeOrderAggregate.setActivityAccountDayEntity(activityAccountDayEntity);

        return createPartakeOrderAggregate;
    }

    @Override
    protected UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate) {
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);
        if (activityEntity == null) {
            throw new AppException(ResponseCode.ACTIVITY_NOT_EXIST_ERROR.getCode(), ResponseCode.ACTIVITY_NOT_EXIST_ERROR.getInfo());
        }
        return UserRaffleOrderEntity.builder()
                .activityId(activityEntity.getActivityId())
                .activityName(activityEntity.getActivityName())
                .userId(userId)
                .orderId(RandomStringUtils.randomNumeric(12))
                .orderTime(currentDate)
                .strategyId(activityEntity.getStrategyId())
                .orderState(UserRaffleOrderStateVO.create)
                .endDateTime(activityEntity.getEndDateTime())
                .build();
    }

    /**
     * 根据用户id和活动id查询活动账户实体
     *
     * @param userId
     * @param activityId
     */
    @Override
    public ActivityAccountEntity queryActivityAccountEntity(String userId, Long activityId) {
        return activityRepository.queryActivityAccountEntity(userId, activityId);
    }
}
